package gdao

import (
	"RedWork/model"
	"fmt"
	"gorm.io/gorm"
	"log"
)
import _ "github.com/go-sql-driver/mysql"

//这个里面返回MySQL的err返回原生的
func AddUser(user model.UserInfo) (uid int, err error) {
	err = nil
	//sqlStr := `insert into user_info(user_name, pass_word, phonenum, email, user_image)
	//			values (?,?,?,?,?)`
	var newuser model.UserInfo
	newuser.UserName = user.UserName
	newuser.PassWord = user.PassWord
	newuser.PhoneNum = user.PhoneNum
	newuser.Email = user.Email
	create := gdb.Table("user_info").Create(&user)
	if create.Error != nil {
		fmt.Println(create.Error.Error())
		return 0, create.Error
	}

	return user.UserId, create.Error
}

func QueryUserID(username string) (uid int, err error) {
	err = nil
	//sqlStr := "select user_id from user_info where user_name = ?"
	//err = DB.QueryRow(sqlStr, username).Scan(&uid)
	//if err != nil {
	//	return 0, err
	//}
	//return
	var user model.UserInfo
	if err = gdb.Table("user_info").Where("user_name = ?", username).First(&user).Error; err != nil {
		log.Println(err)
		return 0, err
	}
	return user.UserId, nil
}

//
func QueryUserPW(username string) (password string, uid int, err error) {
	uid = 0
	password = ""
	err = nil
	fmt.Println(username)
	//sqlStr := fmt.Sprintf("select user_id, pass_word from user_info where user_name='%s'", username)
	//err = DB.QueryRow(sqlStr).Scan(&uid, &password)
	//fmt.Println(uid, password)
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	//return
	fmt.Println("________________________________-")
	var user model.UserInfo
	if err = gdb.Table("user_info").Where("user_name = ?", username).First(&user).Error; err != nil {
		log.Println(err)
		fmt.Println(user)
		return user.PassWord, user.UserId, err
	}
	return user.PassWord, user.UserId, err
}

//
func AddMoney(uid int, money string) (err error) {
	//sqlStr := "UPDATE user_info SET user_info.money = user_info.money + ? WHERE `user_id` = ?"
	//_, err = DB.Exec(sqlStr, money, uid)
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	//fmt.Println(sqlStr)
	if err = gdb.Table("user_info").Where("user_id = ?", uid).Update("money", gorm.Expr("money + ?", money)).Error; err != nil {
		log.Println(err)
		return err
	}
	return
}

//
type UserInfoRes struct {
	UserID   int     `json:"user_id"`
	UserName string  `json:"user_name"`
	PhoneNum int     `json:"phone_num"`
	Email    string  `json:"email"`
	Image    string  `json:"image"`
	Money    float64 `json:"money"`
}

//
func QueryUserInfo(uid string) (info UserInfoRes) {
	//sqlStr := "SELECT user_id, user_name, phonenum, email, user_image, money FROM user_info WHERE user_id = ?"
	//row := DB.QueryRow(sqlStr, uid)
	//err := row.Scan(
	//	&info.UserID,
	//	&info.UserName,
	//	&info.PhoneNum,
	//	&info.Email,
	//	&info.Image,
	//	&info.Money,
	//)
	var user model.UserInfo
	if err := gdb.Table("user_info").Where("user_id = ?", uid).First(&user); err != nil {
		log.Println(err)
		return UserInfoRes{}
	}
	info.UserID = user.UserId
	info.Money = user.Money
	info.Image = user.UserImage
	info.Email = user.Email
	info.UserName = user.UserName
	info.PhoneNum = user.PhoneNum
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	return
}

func ChangePassword(username, newPassword string) (err error) {
	err = gdb.Table("user_info").
		Where("user_name = ?", username).
		Update("pass_word", newPassword).Error
	return err
}
